package com.ls.lserp.modules.material.service;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.CollectionsUtils;
import com.ls.lserp.common.utils.Encodes;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.material.dao.InventoryRequestExDao;
import com.ls.lserp.modules.material.entity.InventoryRequest;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.material.entity.MaterialRequestEx;
import com.ls.lserp.modules.material.entity.RequestExMaterial;
import com.ls.lserp.modules.order.entity.StyleOptionEntity;

/**
 * @ClassName: InventoryRequestService
 * @Description: 物料入库申请
 * 
 * @author 
 * @date 2017年04月14日
 *
 */

@Service
@Transactional(readOnly = false)
public class InventoryRequestExService extends CrudService<InventoryRequestExDao,InventoryRequestEx>{

    @Autowired
    private InventoryRequestExDao inventoryRequestExDao;
    
	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param orderMateriel
	 * @return Page<OrderMateriel>
	 */
	public Page<InventoryRequestEx> findList(Page<InventoryRequestEx> page, InventoryRequestEx inventoryRequestEx) {
		
		
		// tab页分，检索条件设定(0:全部,1:待出库,2:已出库)
		inventoryRequestEx.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
		
		// 出库类别（生产出库）
		inventoryRequestEx.setInventoryCategory(ResourcesUtil.getConfig("outbound_category_produce"));
		
		// 设置分页参数
		inventoryRequestEx.setPage(page);
		List<InventoryRequestEx> resultList = inventoryRequestExDao.findList(inventoryRequestEx);
        // 执行分页查询
        page.setList(resultList);
        
        return page;
	}
	
	/**
	 * check BigDecimal
	 * @param cost
	 * @return 取不到返回0
	 */
	public BigDecimal checkBigDecimalNull(BigDecimal cost){
		if(cost==null){
			return new BigDecimal("0");
		}
		return cost;
	}
	
    /**
     * @Description: 查询
     * @param @return
     * @return InventoryRequestEx
     * @throws
     */
	   public InventoryRequestEx getProductionInfo(InventoryRequestEx inventoryRequestEx) {
		   List<InventoryRequestEx> findMaterial = inventoryRequestExDao.findMaterial(inventoryRequestEx);
		   inventoryRequestEx.setRequestDetailsList(findMaterial);
	       return inventoryRequestEx;
	   }
   
	   /**
	    * @Title: findBrand
	    * @Description: 查询
	    * @param @param venderId
	    * @param @return
	    * @return List<InventoryRequestEx>
	    * @throws
	    */
	  public List<InventoryRequestEx> findProductionList() {
	      return inventoryRequestExDao.findProductionList();
	  }
	  
	  /**
	   * @Title: saveInventoryRequest
	   * @Description: 保存入库申请
	   * @param @param inventoryRequest
	   * @return void 
	   * @throws
	   */
	  @Transactional(readOnly = false)
	  public void saveInventoryRequest(InventoryRequestEx inventoryRequestEx) {
	  	
	  	// 判断新建编辑
	  	//若为null 执行插入语句（新建）
	  	if (inventoryRequestEx.getId()==null){			
	  		inventoryRequestEx.preInsert();
	  		inventoryRequestEx.setVersion(1L);
	  		inventoryRequestEx.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
	  		inventoryRequestEx.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
	  		inventoryRequestEx.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
	  		inventoryRequestEx.setInventoryInOutKbn(ResourcesUtil.getConfig("inventory_in_out_kbn_out"));
	  		inventoryRequestExDao.insertInventoryRequest(inventoryRequestEx);
	  	}else{
			// 更新数据
	  		inventoryRequestEx.preUpdate();
	  		inventoryRequestExDao.updateInventoryRequest(inventoryRequestEx);
			// 删除入库单明细
	  		inventoryRequestExDao.deleteRequestDetails(inventoryRequestEx);
	  	}
	  	
	  	
	  	// 判断list是否为空
	  	if(!CollectionsUtils.isEmpty(inventoryRequestEx.getRequestDetailsList())){
	  	   // 遍历赋值
	         for(InventoryRequestEx itr : inventoryRequestEx.getRequestDetailsList()){
	        	 itr.setId(inventoryRequestEx.getId());
	        	 itr.preInsert();
	        	 itr.setVersion(1L);
	        	 itr.setDeleteFlag(InventoryRequest.DEL_FLAG_NORMAL);
	        	 itr.setEnableFlag(InventoryRequest.ENABlE_FLAG_NORMAL);
	        	 itr.setStockinStatus(ResourcesUtil.getConfig("outbound_status_wait"));
	        	 inventoryRequestExDao.insertInventoryRequestDetails(itr);
	  		}
	  	}
	  }
	  
	  /**
	   * @Title: findRequestDetailsList
	   * @Description: 入库申请明细
	   * @param @param inventoryRequest
	   * @param @return
	   * @return List<InventoryRequest>
	   * @throws
	   */
	 
	 public List<InventoryRequestEx> findRequestDetail(InventoryRequestEx inventoryRequestEx) {
	     return inventoryRequestExDao.findRequestDetail(inventoryRequestEx);
	 }
	 
	 /**
	  * 制作单耗list供导出用
	  * @param styleOptionEntity
	  * @param msg
	  * @return
	  * @throws ParseException 
	  */
	 public List<MaterialRequestEx> findRequestMaterial(RequestExMaterial requestExMaterial){
		 List<MaterialRequestEx> requestExs = inventoryRequestExDao.findEXMaterial(requestExMaterial);
		 DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
		 df.setMaximumFractionDigits(2);
		 for (int i = 0; i < requestExs.size(); i++) {
			 requestExs.get(i).setMaterialCode(Encodes.unescapeHtml(requestExs.get(i).getMaterialCode()));
			 requestExs.get(i).setOrderByNum((i+1)+"");
			 requestExs.get(i).setInventoryInOutKbn("2:出库");
			 requestExs.get(i).setOutQuantity(df.format(Double.parseDouble(requestExs.get(i).getOutQuantity())) + "");
		 }
	     return requestExs;
	 }
	 
	 /**
	  * 获取定制合同小信息 
	  * 
	  * @return
	  */
	 public List<StyleOptionEntity> findContractInfo(){
	 	return inventoryRequestExDao.findContractInfo();
	 }
	 
	 /**
	  * 获取出库申请待出库数量
	  * 
	  * @return
	  */
	 public int getNotOutboundNum(InventoryRequestEx inventoryRequestEx){
		// 设置 查询条件 出库状态为待生产
		inventoryRequestEx.setInventoryStatus(ResourcesUtil.getConfig("outbound_status_wait"));
		// 设置 查询条件 出库类别不为外发出库
		inventoryRequestEx.setInventoryCategory(ResourcesUtil.getConfig("outbound_category_putting"));
	 	return inventoryRequestExDao.getNotOutboundNum(inventoryRequestEx);
	 }
}